package com.wlyuan.open.assembler;

import com.wlyuan.area.dto.AnalyzerAddressResultDTO;
import com.wlyuan.basic.dto.consignee.ReceivingAddressCreationDTO;
import com.wlyuan.basic.dto.consignee.ReceivingAddressUpdateDTO;
import com.wlyuan.basic.dto.customer.ShippingAddressCreationDTO;
import com.wlyuan.basic.dto.customer.ShippingAddressUpdateDTO;
import com.wlyuan.open.basic.dto.OpenReceivingAddressCreationDTO;
import com.wlyuan.open.basic.dto.OpenReceivingAddressUpdateDTO;
import com.wlyuan.open.basic.dto.OpenShippingAddressCreationDTO;
import com.wlyuan.open.basic.dto.OpenShippingAddressUpdateDTO;
import lombok.var;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
 * @author
 * 地址 OpenDTO -> 业务DTO
 */
public class AddressAssembler {
    @Mapper(uses = {

    })
    public interface Assembler {

        ShippingAddressCreationDTO assemble(OpenShippingAddressCreationDTO openShippingAddress);

        ShippingAddressUpdateDTO assemble(OpenShippingAddressUpdateDTO openShippingAddress);

        ReceivingAddressCreationDTO assemble(OpenReceivingAddressCreationDTO source);

        ReceivingAddressUpdateDTO assemble(OpenReceivingAddressUpdateDTO source);

    }

    private static final Assembler ASSEMBLER = Mappers.getMapper(Assembler.class);

    public static ShippingAddressCreationDTO assemble(OpenShippingAddressCreationDTO openShippingAddress, AnalyzerAddressResultDTO addressResult){
        var creation = ASSEMBLER.assemble(openShippingAddress);
        if(addressResult!=null){
            creation.setProvinceCode(addressResult.getProvinceCode());
            creation.setCityCode(addressResult.getCityCode());
            creation.setDistrictCode(addressResult.getDistrictCode());
        }
        return creation;
    }

    public static ShippingAddressUpdateDTO assemble(OpenShippingAddressUpdateDTO openShippingAddress, AnalyzerAddressResultDTO addressResult){
        var creation = ASSEMBLER.assemble(openShippingAddress);
        if(addressResult!=null){
            creation.setProvinceCode(addressResult.getProvinceCode());
            creation.setCityCode(addressResult.getCityCode());
            creation.setDistrict(addressResult.getDistrictCode());
        }
        return creation;
    }

    public static ReceivingAddressCreationDTO assemble(OpenReceivingAddressCreationDTO openReceivingAddress, AnalyzerAddressResultDTO addressResult){
        var creation = ASSEMBLER.assemble(openReceivingAddress);
        if(addressResult!=null){
            creation.setProvinceCode(addressResult.getProvinceCode());
            creation.setCityCode(addressResult.getCityCode());
            creation.setDistrictCode(addressResult.getDistrictCode());
        }
        return creation;
    }

    public static ReceivingAddressUpdateDTO assemble(OpenReceivingAddressUpdateDTO openShippingAddress, AnalyzerAddressResultDTO addressResult){
        var creation = ASSEMBLER.assemble(openShippingAddress);
        if(addressResult!=null){
            creation.setProvinceCode(addressResult.getProvinceCode());
            creation.setCityCode(addressResult.getCityCode());
            creation.setDistrict(addressResult.getDistrictCode());
        }
        return creation;
    }
}
